/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.product.web.dict;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.product.entity.dict.TbProductAttributeDict;
import com.thinkgem.jeesite.modules.product.service.dict.TbProductAttributeDictService;

/**
 * 商品属性字典Controller
 *
 * @author 宋子军
 * @version 2016-11-21
 */
@Controller
@RequestMapping(value = "${adminPath}/product/dict/tbProductAttributeDict")
public class TbProductAttributeDictController extends BaseController {

    @Autowired
    private TbProductAttributeDictService tbProductAttributeDictService;

    @ModelAttribute
    public TbProductAttributeDict get(@RequestParam(required = false) String id) {
        TbProductAttributeDict entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = tbProductAttributeDictService.get(id);
        }
        if (entity == null) {
            entity = new TbProductAttributeDict();
        }
        return entity;
    }

    @RequestMapping(value = {"list", ""})
    public String list(TbProductAttributeDict tbProductAttributeDict, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<TbProductAttributeDict> page = tbProductAttributeDictService.findPage(new Page<TbProductAttributeDict>(request, response), tbProductAttributeDict);
        model.addAttribute("page", page);
        return "modules/product/dict/tbProductAttributeDictList";
    }

    @RequestMapping(value = "form")
    public String form(TbProductAttributeDict tbProductAttributeDict, Model model) {
        model.addAttribute("tbProductAttributeDict", tbProductAttributeDict);
        return "modules/product/dict/tbProductAttributeDictForm";
    }

    @RequestMapping(value = "save")
    public String save(TbProductAttributeDict tbProductAttributeDict, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, tbProductAttributeDict)) {
            return form(tbProductAttributeDict, model);
        }
        tbProductAttributeDictService.save(tbProductAttributeDict);
        addMessage(redirectAttributes, "保存商品属性字典成功");
        return "redirect:" + Global.getAdminPath() + "/product/dict/tbProductAttributeDict/?repage";
    }

    @RequestMapping(value = "delete")
    public String delete(TbProductAttributeDict tbProductAttributeDict, RedirectAttributes redirectAttributes) {
        tbProductAttributeDictService.delete(tbProductAttributeDict);
        addMessage(redirectAttributes, "删除商品属性字典成功");
        return "redirect:" + Global.getAdminPath() + "/product/dict/tbProductAttributeDict/?repage";
    }


    @RequestMapping(value = "/children")
    @ResponseBody
    public List<TbProductAttributeDict> getList(TbProductAttributeDict tbProductAttributeDict) {
        return this.tbProductAttributeDictService.findList(tbProductAttributeDict);
    }

}